package br.com.karitza.gui;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import br.com.karitza.gui.R;
import br.com.karitza.modelo.entidade.Caracteristica;
import br.com.karitza.modelo.entidade.Imovel;

public class ListaCaracteristicas extends Activity implements OnClickListener {
	private LinearLayout lyPersonalizado;
	private ArrayList<CheckBox> checks = new ArrayList<CheckBox>();
	private ArrayList<Caracteristica> caracteristicas;
	private Imovel imovel;
	private String titulo;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		imovel = (Imovel)getIntent().getSerializableExtra("imovel");
		titulo = getIntent().getStringExtra("titulo");
		caracteristicas = imovel.getCaracteristicas(titulo);
		
		lyPersonalizado = new LinearLayout(this);
		lyPersonalizado.setOrientation(LinearLayout.VERTICAL);

		ScrollView s = new ScrollView(this);
		s.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));

		TextView text = new TextView(this);
		text.setText(titulo);
		text.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.rh_mini, 0);
		text.setBackgroundColor(Color.GRAY);
		lyPersonalizado.addView(text);
		
		for (int c = 0; c < caracteristicas.size(); c++) {
			//Arraylist com o mesmo tamanho da lista de checkbox.
			//selecionados.add(caracteristicas.get(c));
			CheckBox cb = new CheckBox(this);
			cb.setText( caracteristicas.get(c).getNome() );
			cb.setId( caracteristicas.get(c).getId() );
			cb.setChecked( caracteristicas.get(c).isMarcado() );
			cb.setOnClickListener(this);
			checks.add(cb);
			lyPersonalizado.addView(cb);
		}

		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		s.addView(lyPersonalizado);
		setContentView(s);
	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			Intent intent= getIntent();
			String msg = intent.getStringExtra("param");
			msg += ", Adicionado depois";
			intent.putExtra("retorno", msg);
			setResult(RESULT_OK, intent);
			finish();
		}
		return super.onKeyDown(keyCode, event);
	}

	public void onClick(View v) {
		for (int i=0; i < checks.size(); i++) {
			caracteristicas.get(i).setMarcado( checks.get(i).isChecked() );
		}
	}
}
